#!/bin/bash
DOWNLOAD_DIR="/storage/download" #下载的路径
INSTALL_DIR="/storage/local"   #安装的路径
PYTHON_VER="3.10.19"
PYTHON_SRC="https://www.python.org/ftp/python/${PYTHON_VER}/Python-${PYTHON_VER}.tgz" #python的安装包地址
LOG_FILE="${DOWNLOAD_DIR}/install_python.log" # 日志文件
rpm -q yum &>/dev/null
if [ $? -ne 0 ]; then
    echo "请先安装yum工具"
    exit
fi
rpm -q wget &>/dev/null
if [ $? -ne 0 ]; then
    yum install wget -y
fi
#如果源文件不存在则下载
if [ ! -f "${DOWNLOAD_DIR}/Python-${PYTHON_VER}.tgz" ]; then
    echo $(date +"%Y-%m-%d %H:%M:%S") ":开始下载python源文件;" >>${LOG_FILE}
    cd ${DOWNLOAD_DIR}
    wget -c $PYTHON_SRC
fi
#如果源文件存在，
if [ -f "${DOWNLOAD_DIR}/Python-${PYTHON_VER}.tgz" ]; then
    #则检测是否已解压
    if [ ! -d "${DOWNLOAD_DIR}/Python-${PYTHON_VER}" ]; then
        cd ${DOWNLOAD_DIR}
        tar -zxvf Python-${PYTHON_VER}.tgz
    fi
fi
cd ${DOWNLOAD_DIR}/Python-${PYTHON_VER}
./configure --prefix=$INSTALL_DIR/python$PYTHON_VER # --enable-optimizations (需要优化可加这个参数-对内存有要求)
if [ $? -eq 0 ]; then
    echo $(date +"%Y-%m-%d %H:%M:%S") ":开始安装python;" >>${LOG_FILE}
    make && make install
    if [ $? -eq 0 ]; then
        echo $(date +"%Y-%m-%d %H:%M:%S")":python ${PYTHON_VER} 安装完成" >>${LOG_FILE}
        #软连接
        ln -s $INSTALL_DIR/python$PYTHON_VER/bin/python3.10  /usr/local/bin/py3
    else
        echo $(date +"%Y-%m-%d %H:%M:%S")":python ${PYTHON_VER} 安装失败.原因>" 2>>${LOG_FILE}
    fi
else
    echo $(date +"%Y-%m-%d %H:%M:%S") ":安装出错。出错信息为:" 2>>${LOG_FILE}
    exit 1
fi

##########删除##########
# 按照实际安装的路径进行删除即可
# rm -rf /usr/local/python3 #删除安装目录
# rm /usr/bin/pip3 #删除软链接
#######################
#########安装软件eg##########
# py3 -m pip install --upgrade pip -i http://mirrors.cloud.tencent.com/pypi/simple --trusted-host mirrors.cloud.tencent.com
###################

